<?php
// Open Header, include functions and get the showid and cached xml
include 'functions.php';
loadheader();
$sid = $_GET['sid'];

$cache_time = 3600*24; // 24 hours

$cache_file = './cache/'.$sid.'-eplist.xml';
$timedif = @(time() - filemtime($cache_file));

if (file_exists($cache_file) && $timedif < $cache_time) {
    $string = file_get_contents($cache_file);
} else {
    $string = file_get_contents('http://services.tvrage.com/feeds/full_show_info.php?sid='.$sid);
    if (($string) && ($f = @fopen($cache_file, 'w'))) {
        fwrite ($f, $string, strlen($string));
        fclose($f);
    }
}
// Cached the file, now lets use it.
if (!file_exists($cache_file)) { // Does it exist? If it doesn't there's a problem so lets report it.
 echo "<center><h2>Cache file issue: Cannot open cached XML poss connection issue?</h2></center>";
 break;
}
$xml = simplexml_load_file($cache_file);
echo "<table border=0 width=800px; cellpadding=2>\n";
$image = $xml->image;
if ($image == "") {$image = "./style/images/noimg.png";}
echo "<tr><td><img src=\"".$image."\" align=center></img></td><td>";
echo "<table border=0 width=400px; cellpadding=2>\n";
echo "<tr><td><a class=\"showtitle\" href=\"".$xml->showlink."\">".$xml->name."</a></td></tr>\n";
echo "<tr><td><div class=\"totalseasons\">Total Seasons Aired: ".$xml->totalseasons . "</div></td></tr>\n";
echo "<tr><td><ul><li>Started: ".$xml->started."</li><li>Ended: ".$xml->ended."</li><li>Current Status: ".$xml->status."</li><li>Runtime: ".$xml->runtime."</li><li>Network: ".$xml->network['country']."; ".$xml->network."</li>";
echo "<li>Airs: ".$xml->airday." @ ".$xml->airtime."</li><li>Classification: ".$xml->classification."</li>";
echo "</ul></td></tr></table></td></tr></table>";
echo "<table border=0 width=600px; cellpadding=2>\n";
echo "<tr><th width=30px;>Ep #</th><th width=70px;>Season<br>Ep#</th><th width=80px;>Production<br>Number</th><th width=80px;>Original<br>Air Date</th><th>Title</th></tr>\n";
echo "<tr></tr>\n";

if ($xml->Episodelist->Season == "") {echo "<tr><th colspan=\"5\">No episode information to display on this schedule.</th></tr>";}
else {
 foreach($xml->Episodelist->Season as $season){
 	echo "<tr><th colspan=\"5\"><b>Season: ".$season['no']."</b></th></tr>\n";
 	foreach($season->episode as $node){
 		echo "<tr><td>".$node->epnum."</td>\n";
 		echo "<td>".$node->seasonnum."</td>\n";
 		echo "<td>".$node->prodnum."</td>\n";
		echo "<td>".$node->airdate."</td>\n";
 	  echo "<td><a href=\"".$node->link."\">".make_safe($node->title)."</a></td></tr>\n";
 	}
 }
}
if ($xml->Episodelist->Special) {
	foreach($xml->Episodelist->Special as $special){
  	echo "<tr><th colspan=\"5\"><b>Specials: \n</b></th></tr>\n";
  	foreach($special->episode as $node){
  		echo "<tr><td>".$node->epnum."</td>\n";
  		echo "<td>".$node->seasonnum."</td>\n";
  		echo "<td>".$node->prodnum."</td>\n";
 	  	echo "<td>".$node->airdate."</td>\n";
  	  echo "<td><a href=\"".$node->link."\">".make_safe($node->title)."</a></td></tr>\n";
  	}
  }
}
echo "</table>\n";
echo "<p><div align=center>Page generated using a cached feed generated on: ".date ("F d Y H:i:s.", filemtime($cache_file))."</div></p>\n";
loadfooter();
?> 